package com.waves.message.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waves.core.constant.DbConstants;
import com.waves.core.support.EntityOperations;
import com.waves.message.dto.MessageRecordInsertDTO;
import com.waves.message.dto.MessageRecordPageDTO;
import com.waves.message.entity.MessageRecord;
import com.waves.message.events.ISendMessageEvent;
import com.waves.message.mapper.MessageRecordMapper;
import com.waves.message.service.MessageRecordService;
import com.waves.message.vo.MessageRecordPageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

/**
 * @author wzw
 * @description 针对表【message_record】的数据库操作Service实现
 * @createDate 2025-01-01 15:01:07
 */
@Service
@RequiredArgsConstructor
public class MessageRecordServiceImpl extends ServiceImpl<MessageRecordMapper, MessageRecord>
        implements MessageRecordService {

    private final ApplicationEventPublisher applicationEventPublisher;

    @Override
    public IPage<MessageRecordPageVO> pageMessageRecords(MessageRecordPageDTO messageRecordPageDTO) {
        return baseMapper.selectPage(messageRecordPageDTO, Wrappers.lambdaQuery(MessageRecord.class)
                                                                   .eq(Objects.nonNull(
                                                                               messageRecordPageDTO.getIsRead()),
                                                                       MessageRecord::getIsRead,
                                                                       messageRecordPageDTO.getIsRead())
                                                                   .eq(Objects.nonNull(
                                                                               messageRecordPageDTO.getReceptionUserId()),
                                                                       MessageRecord::getReceptionUserId,
                                                                       messageRecordPageDTO.getReceptionUserId())
                                                                   .eq(Objects.nonNull(
                                                                               messageRecordPageDTO.getNoticeType()),
                                                                       MessageRecord::getNoticeType,
                                                                       messageRecordPageDTO.getNoticeType())
                                                                   .eq(Objects.nonNull(
                                                                               messageRecordPageDTO.getMessageType()),
                                                                       MessageRecord::getMessageType,
                                                                       messageRecordPageDTO.getMessageType())
                                                                   .like(Objects.nonNull(
                                                                                 messageRecordPageDTO.getTitle()),
                                                                         MessageRecord::getTitle,
                                                                         messageRecordPageDTO.getTitle())
        );
    }

    @Override
    public Long insert(MessageRecordInsertDTO messageRecordInsertDTO) {
        MessageRecord entity = new MessageRecord();
        BeanUtils.copyProperties(messageRecordInsertDTO, entity);
        Optional<MessageRecord> execute = EntityOperations.doCreate(baseMapper)
                                                          .create(() -> entity)
                                                          .update(e -> {})
                                                          .successHook(
                                                                  thisEntity -> applicationEventPublisher.
                                                                          publishEvent(
                                                                                  new ISendMessageEvent.insertEvent(
                                                                                          thisEntity)))
                                                          .execute();
        return execute.map(MessageRecord::getId)
                      .orElse(DbConstants.DEFAULT_LONG_VALUE);
    }
}




